package com.zzptc.sky.baidudemo.activities;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.sky.baidudemo.R;
import com.zzptc.sky.baidudemo.base.BaseActivity;
import com.zzptc.sky.baidudemo.fragments.SOSForSelectContactsFragment;
import com.zzptc.sky.baidudemo.fragments.SOSForSelectContactsFragment2;
import com.zzptc.sky.baidudemo.utils.MyApplication;
import com.zzptc.sky.baidudemo.utils.MyUntils;
import com.zzptc.sky.baidudemo.vo.ContactsInfo;

import java.util.ArrayList;
import java.util.List;

public class SOSForSelectContactsActivity extends BaseActivity {

    private Toolbar tl_select_contacts;
    private EditText et_select;
    private SOSForSelectContactsFragment2 sosForSelectContactsFragment2;
    private boolean flag = true;
    private List<ContactsInfo> list;
    private List<ContactsInfo> newlist = new ArrayList<ContactsInfo>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_sos_select_contacts);

        tl_select_contacts = (Toolbar) findViewById(R.id.tl_select_contacts);
        et_select = (EditText) findViewById(R.id.et_select);

        setSupportActionBar(tl_select_contacts);

        list = MyApplication.getContactsList();

        //碎片布局管理
        getFragmentManager().beginTransaction().replace(R.id.fl_contacts,new SOSForSelectContactsFragment()).addToBackStack("contacts").commit();

        //设置改变监听
        et_select.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (flag){
                    if (sosForSelectContactsFragment2  == null){
                        sosForSelectContactsFragment2 = new SOSForSelectContactsFragment2();
                    }
                    getFragmentManager().beginTransaction().replace(R.id.fl_contacts,sosForSelectContactsFragment2).addToBackStack("contacts").commit();
                    flag = false;
                }

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                final CharSequence c=s.toString().replace(" ","").replace("-","");
                newlist.clear();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int x = 0; x<list.size();x++){
                            ContactsInfo info = list.get(x);
                            if (info.getName().contains(c) || info.getPhone().toString().replace(" ","").replace("-","").contains(c)){
                                newlist.add(info);
                            }
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                sosForSelectContactsFragment2.getContactsList(newlist);
                            }
                        });
                    }
                }).start();

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0){
                    if (getFragmentManager().getBackStackEntryCount() > 1){
                        getFragmentManager().popBackStack();
                    }
                    flag = true;
                }
            }
        });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_sos_select_contacts;
    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 1){
            getFragmentManager().popBackStack();
        }else {
            finish();
        }
    }
}
